﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;

namespace N_m3u8DL_CLI_SimpleG
{
    public partial class MainWindow
    {
        const string PATTERN_M3U8 = ".m3u8";
        const string PATTERN_URL = @"\b(https?|ftp|file)://[-A-Z0-9+&@#/%?=~_|$!:,.;]*[A-Z0-9+&@#/%=~_|$]";

        ClipboardMonitor FClipboardMonitor = new ClipboardMonitor();
        string[] FClipboardHistory = new string[2];
        bool[] FClipboardModified = new bool[2];

        /// <summary>
        /// 监控剪贴板
        /// </summary>
        /// <param name="e"></param>
        protected override void OnSourceInitialized(EventArgs e)
        {
            base.OnSourceInitialized(e);

            CheckBox_URL.IsChecked = true;
            CheckBox_Title.IsChecked = true;
            CheckBox_AutoStart.IsChecked = true;
            
            Button_GO_Click_Ext(null, new RoutedEventArgs());
            Button_GO.Click += Button_GO_Click_Ext;

            FClipboardMonitor.ContentChanged += ClipboardMonitor_ContentChanged;
            FClipboardMonitor.Start(this);
        }

        /// <summary>
        /// 移除剪贴板监控
        /// </summary>
        /// <param name="e"></param>
        protected override void OnClosing(CancelEventArgs e)
        {
            base.OnClosing(e);
            if (!e.Cancel)
            {
                Button_GO.Click -= Button_GO_Click_Ext;
                FClipboardMonitor.Stop();
            }
        }

        /// <summary>
        /// 剪贴板内容变更
        /// </summary>
        /// <param name="e"></param>
        private void ClipboardMonitor_ContentChanged(ClipboardMonitor.ClipboardEventArgs e)
        {
            var text = e.Text.Trim();
            if (string.IsNullOrWhiteSpace(text) || CheckBox_AutoStart.IsChecked != true)
                return;
            
            if (/* e.Text.EndsWith(PATTERN_M3U8, StringComparison.OrdinalIgnoreCase) && */ Regex.IsMatch(text, PATTERN_URL, RegexOptions.IgnoreCase) && CheckBox_URL.IsChecked == true)
            {
                TextBox_URL.Text = text;
                TextBox_URL.Focus();
                TextBox_URL.SelectAll();
                CheckAutoStart(0, text);
            }
            else if (CheckBox_Title.IsChecked == true)
            {
                TextBox_Title.Text = text;
                TextBox_Title.Focus();
                TextBox_Title.SelectAll();
                CheckAutoStart(1, text);
            }
        }

        /// <summary>
        /// 清除自动下载状态
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void Button_GO_Click_Ext(object sender, RoutedEventArgs e)
        {
            for (var i = 0; i < FClipboardModified.Length; i++)
                FClipboardModified[i] = false;
        }

        /// <summary>
        /// 检查是否需要开始下载
        /// </summary>
        /// <param name="index"></param>
        /// <param name="text"></param>
        void CheckAutoStart(int index, string text)
        {
            if (FClipboardHistory[index] != text)
            {
                FClipboardHistory[index] = text;
                FClipboardModified[index] = true;
            }

            for (var i = 0; i < FClipboardModified.Length; i++)
                if (!FClipboardModified[i])
                    return;

            Button_GO.RaiseEvent(new RoutedEventArgs(System.Windows.Controls.Primitives.ButtonBase.ClickEvent, Button_GO));
        }
    }
}
